package com.hiwhitley.himei.module.gank;

import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.hiwhitley.himei.R;
import com.hiwhitley.himei.contract.GankContract;
import com.hiwhitley.himei.contract.model.GankModel;
import com.hiwhitley.himei.contract.presenter.GankPresenter;
import com.hiwhitley.himei.entity.GankItem;
import com.hiwhitley.himei.entity.GankItemGirl;
import com.hiwhitley.himei.entity.GankItemTitle;
import com.hiwhitley.himei.mvpframe.base.BaseFrameFragment;
import com.hiwhitley.himei.widget.glide.GlideImageView;
import com.hiwhitley.himei.widget.pinnedsectionitemdecoration.OnHeaderClickListener;
import com.hiwhitley.himei.widget.pinnedsectionitemdecoration.PinnedHeaderItemDecoration;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.header.SinaRefreshView;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import me.drakeet.multitype.MultiTypeAdapter;

/**
 * Created by hiwhitley on 17-2-14.
 */

public class GankFragment extends BaseFrameFragment<GankPresenter, GankModel> implements GankContract.View {
    @BindView(R.id.gank_recycler_view)
    RecyclerView mGankRecyclerView;
    @BindView(R.id.twinkling_refresh_layout)
    TwinklingRefreshLayout mSwipeRefreshLayout;

    private MultiTypeAdapter mMultiTypeAdapter;
    private List<GankItem> mGankItems;

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_gank;
    }

    @Override
    protected void init() {
        mGankItems = new ArrayList<>();

        mMultiTypeAdapter = new MultiTypeAdapter();
        mMultiTypeAdapter.register(GankItemGirl.class, new GankItemGirlViewProvider());
        mMultiTypeAdapter.register(GankItemTitle.class, new GankItemTitleViewProvider());
        mMultiTypeAdapter.register(GankItem.class, new GankItemViewProvider());

        mPresenter.onRefresh();
    }

    @Override
    protected void setListener() {

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        mGankRecyclerView.setLayoutManager(linearLayoutManager);
        mGankRecyclerView.addItemDecoration(new PinnedHeaderItemDecoration.Builder(GankItem.TYPE_GIRL).setHeaderClickListener(new OnHeaderClickListener() {
            @Override
            public void onHeaderClick(View view, int id, int position) {
                GlideImageView girl = (GlideImageView) view.findViewById(R.id.giv_girl);
                girl.loadNetImage(mGankItems.get(position).url, R.drawable.bg_holder_brown);
                PhotoViewsActivity.startActivity(getContext(), mGankItems.get(position).url);
            }

            @Override
            public void onHeaderLongClick(View view, int id, int position) {

            }

            @Override
            public void onHeaderDoubleClick(View view, int id, int position) {

            }
        }).create());

        mGankRecyclerView.setAdapter(mMultiTypeAdapter);

        mSwipeRefreshLayout.setHeaderView(new SinaRefreshView(getContext()));
        mSwipeRefreshLayout.setOverScrollBottomShow(true);
        mSwipeRefreshLayout.setAutoLoadMore(true);
        mSwipeRefreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onLoadMore(TwinklingRefreshLayout refreshLayout) {
                (new Handler()).postDelayed(() -> {
                    mPresenter.onLoadingMore();
                    refreshLayout.finishLoadmore();
                }, 2000);
            }

            @Override
            public void onRefresh(TwinklingRefreshLayout refreshLayout) {
                (new Handler()).postDelayed(() -> {
                    mPresenter.onRefresh();
                    refreshLayout.finishRefreshing();
                }, 2000);
            }
        });
    }

    @Override
    public void onRequestStart() {

    }

    @Override
    public void onRequestEnd() {

    }

    @Override
    public void showGankDay(List<GankItem> list, boolean isLoadMore) {
        mGankItems = list;
        mMultiTypeAdapter.setItems(list);
        mMultiTypeAdapter.notifyDataSetChanged();
    }
}
